//
//  PhotoBrowser.m
//  Modal图片浏览
//
//  Created by new-1020 on 2017/1/17.
//  Copyright © 2017年 new-1020. All rights reserved.
//

#import "PhotoBrowser.h"


@interface PhotoBrowser()
@property(nonatomic,assign)BOOL isPrsented;

@end

@implementation PhotoBrowser

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.isPrsented = YES;
    return self;
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPrsented = NO;
    return self;
}

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.isPrsented) {
        [self anmiForPrsent:transitionContext];
    }else {
        [self anmiForDismiss:transitionContext];
    }
}

-(void)anmiForPrsent:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    // 取出弹出的view
    UIView * toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    // 将该view添加到containerView中
    [transitionContext.containerView addSubview: toView];
    
    // 执行动画
//    UIImageView * imageView = [self.presentDelegate XMimageView];
//    CGRect startRect = [self.presentDelegate startRectWithIndexpath:self.indexpath];
//    CGRect endRect = [self.presentDelegate endRect];
//    imageView.frame = startRect;
//    [transitionContext.containerView addSubview: imageView];
//    transitionContext.containerView.backgroundColor = [UIColor blackColor];
    toView.alpha = 0.0;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
//        imageView.frame = endRect;
        toView.alpha = 1;
    } completion:^(BOOL finished) {
        // 必须告诉上下文动画执行完毕了
//        [imageView removeFromSuperview];
//        toView.alpha = 1;
        transitionContext.containerView.backgroundColor = [UIColor clearColor];
        [transitionContext completeTransition:YES];
    }];
}

-(void)anmiForDismiss:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    UIView * fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//    UIImageView * imageView = [self.dismissDelegate XMimageViewDismiss];
//    CGRect startRect = [self.presentDelegate startRectWithIndexpath:[self.dismissDelegate IndexpathOfEndCell]];
//    [transitionContext.containerView addSubview: imageView];
    // 取出弹出的view
//    UIView * toView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    // 将该view添加到containerView中
    // 执行动画
    fromView.alpha = 1;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
//        imageView.frame = startRect;
        fromView.alpha = 0;
    } completion:^(BOOL finished) {
        // 必须告诉上下文动画执行完毕了
//        [imageView removeFromSuperview];
        [fromView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}
@end
